package com.qfmy.controller.novel;

import com.qfmy.service.novel.HistoryService;
import com.qfmy.vo.novel.HistoryVo;
import common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author 清风明月
 * @Package com.qfmy.controller.novel
 * @date 2025/6/7
 * @description 浏览历史
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/app/history")
public class HistoryController {

    /**
     * 注入service
     */
    @Autowired
    private HistoryService historyService;

    /**
     * 获取最新的浏览历史
     */
    @RequestMapping("/getNewestHistory")
    public Result<HistoryVo> getNewestHistory() {
       HistoryVo historyVo = historyService.getNewestHistory();
       return Result.ok(historyVo);
    }

    /**
     * 分页获取浏览历史
     */
    @RequestMapping("/getHistoryByPage")
    public Result<List<HistoryVo>> getHistoryByPage(@RequestParam Integer pageNum, @RequestParam Integer size) {
        List<HistoryVo> historyByPage = historyService.getHistoryByPage(pageNum, size);
        return Result.ok(historyByPage);
    }

    /**
     * 清除浏览历史
     */
    @RequestMapping("/clearHistory")
    public Result clearHistory() {
        historyService.clearHistory();
        return Result.ok("清除成功");
    }

    /**
     * 根据浏览历史id清除浏览历史
     */
    @RequestMapping("/clearHistoryById")
    public Result clearHistoryById(@RequestParam Long historyId) {
        historyService.clearHistoryById(historyId);
        return Result.ok();
    }

    /**
     * 保存浏览历史
     */
    @RequestMapping("/saveHistory")
    public Result saveHistory(@RequestParam Long novelId, @RequestParam Long chapterIndex ,@RequestParam Integer page) {
        historyService.saveHistory(novelId, chapterIndex, page);
        return Result.ok();
    }

    /**
     * 根据小说id获取历史记录
     */
    @RequestMapping("/getHistoryByNovelId")
    public Result<HistoryVo> getHistoryByNovelId(@RequestParam Long novelId) {
        HistoryVo historyVo = historyService.getHistoryByNovelId(novelId);
        return Result.ok(historyVo);
    }

}
